package com.yuncheng.spcyApi.spcy;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yuncheng.spcyApi.alog.annotation.Log;
import com.yuncheng.spcyApi.busService.InitWjjlService;
import com.yuncheng.spcyApi.constant.common.LogConstant;
import com.yuncheng.spcyApi.constant.common.SpcyConstant;
import com.yuncheng.spcyApi.constant.sys.SysConfigConstant;
import com.yuncheng.spcyApi.constant.wjml.WjmlConstant;
import com.yuncheng.spcyApi.constant.xcjchy.SqsxHyConstant;
import com.yuncheng.spcyApi.entity.*;
import com.yuncheng.exception.BusinessException;
import com.yuncheng.spcyApi.flow.FlowVo;
import com.yuncheng.spcyApi.flow.JcyRckpHpFlowService;
import com.yuncheng.spcyApi.flow.SpcyFlowConstant;
import com.yuncheng.spcyApi.mapper.SpcyFlowMapper;
import com.yuncheng.spcyApi.mapper.SpcySqsxHydjryMapper;
import com.yuncheng.spcyApi.mapper.YcSysUserMapper;
import com.yuncheng.spcyApi.service.*;
import com.yuncheng.spcyApi.spcyService.SpcyMyInfoService;
import com.yuncheng.spcyApi.spcyService.ZdyUploadFileService;
import com.yuncheng.spcyApi.utils.*;
import com.yuncheng.spcyApi.vo.common.Result;
import com.yuncheng.spcyApi.vo.flow.KpFlowCommonJobVo;
import com.yuncheng.spcyApi.vo.jcy.JcyHpjlResVo;
import com.yuncheng.vo.HttpResult;
import com.yuncheng.spcyApi.vo.common.HttpResultVo;
import com.yuncheng.spcyApi.vo.xcjchy.SqsxHyLxVo;
import com.yuncheng.spcyApi.vo.xcjchy.SqsxHySendYzmVo;
import com.yuncheng.spcyApi.vo.xcjchy.SqsxHySubmitVo;
import io.swagger.annotations.*;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;
import utils.DateConversionUtils;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.*;


@Api(tags = "审评查验-白名单对外接口")
@RestController
@RequestMapping("/api/spzx-spcy/spcySqsxXpjl")
public class SpcyWhiteListController {

    private static final Logger log = LoggerFactory.getLogger(SpcyWhiteListController.class);

    @Resource
    private ZdyUploadFileService zdyUploadFileService;

    @Resource
    private ISpcySqsxHyztService spcySqsxHyztService;

    @Resource
    private ISpcySqsxHymxService spcySqsxHymxService;

    @Resource
    private YcSysUserMapper ycSysUserMapper;

    @Resource
    private ISpcyRsYwzdxxService spcyRsYwzdxxService;

    @Resource
    private ISysConfigService sysConfigService;

    @Resource
    private InitWjjlService initWjjlService;

    @Resource
    private ISpcySqsxBaiscService spcySqsxBaiscService;

    @Resource
    private SmsUtils smsUtils;

    /*@Resource
    private SpcyJcyFybxdgrjlMapper spcyJcyFybxdgrjlMapper;*/

    @Resource
    private SpcySqsxHydjryMapper spcySqsxHydjryMapper;

    @Resource
    private LocalFileUploadUtils localFileUploadUtils;

    @Resource
    private JcyRckpHpFlowService jcyRckpHpFlowService;

    @Resource
    private SpcyFlowMapper spcyFlowMapper;

    @Resource
    private SpcyMyInfoService spcyMyInfoService;

    @Log(title = "获取无利益冲突声明内容")
    @ApiOperation(value = "获取无利益冲突声明内容")
    @GetMapping(value = "queryWlyctsmContent")
    public HttpResult queryWlyctsmContent(@RequestParam(value = "sqsxid",defaultValue = "") String sqsxid){

        String code = "001002";
        if (StringUtils.isNotBlank(sqsxid)) {
            code = Optional.ofNullable(spcySqsxBaiscService.selectSqsxYwflCodeById(sqsxid)).map(f->f.getFywflcode()).orElseGet(()->"001002");
        }

        return HttpResultVo.HttpResultSave(spcyRsYwzdxxService.selectWlyctsmContent(code));
    }

    @Log(title = "获取现场检查检查员承诺书内容")
    @ApiOperation(value = "获取现场检查检查员承诺书内容")
    @GetMapping(value = "queryJcycnsContent")
    public HttpResult queryJcycnsContent(@RequestParam(value = "sqsxid",defaultValue = "") String sqsxid){

        String code = "001002";
        if (StringUtils.isNotBlank(sqsxid)) {
            code = Optional.ofNullable(spcySqsxBaiscService.selectSqsxYwflCodeById(sqsxid)).map(f->f.getFywflcode()).orElseGet(()->"001002");
        }

        return HttpResultVo.HttpResultSave(spcyRsYwzdxxService.selectJcycnsContent(code));
    }

    @Log(title = "获取现场检查纪律内容")
    @ApiOperation(value = "获取现场检查纪律内容")
    @GetMapping(value = "queryXcjcjlContent")
    public HttpResult queryXcjcjlContent(@RequestParam(value = "sqsxid",defaultValue = "") String sqsxid){

        String code = "001002";
        if (StringUtils.isNotBlank(sqsxid)) {
            code = Optional.ofNullable(spcySqsxBaiscService.selectSqsxYwflCodeById(sqsxid)).map(f->f.getFywflcode()).orElseGet(()->"001002");
        }

        return HttpResultVo.HttpResultSave(spcyRsYwzdxxService.selectXcjcjlContent(code));
    }

    @Log(title = "首次会议-验证手机号是否存在系统")
    @ApiOperation(value = "首次会议-验证手机号是否存在系统(0：不存在/1: 存在)")
    @GetMapping(value = "verifyPhoneSfczxt")
    public HttpResult verifyPhoneSfczxt(@RequestParam(value = "phone",defaultValue = "") String phone,
                                        @RequestParam(value = "id",defaultValue = "") String id)  {
        if (StringUtils.isBlank(phone)){
            return HttpResultVo.HttpResultSave("0");
        }

        if (StringUtils.isNotBlank(id)) {
            List<String> userIdList = spcySqsxHydjryMapper.selectFuseridByFsqsxidAndPhone(phone, id);
            if (CollectionUtil.isEmpty(userIdList)) {
                return HttpResultVo.HttpResultSave("0");
            }else {
                return HttpResultVo.HttpResultSave("1");
            }
        }else {

            if (StringUtils.isBlank(ycSysUserMapper.findUserIdByPhone(phone))) {
                return HttpResultVo.HttpResultSave("0");
            } else {
                return HttpResultVo.HttpResultSave("1");
            }
        }
    }

    @Log(title = "现场检查会议-发送验证码")
    @ApiOperation(value = "现场检查会议-发送验证码")
    @PostMapping(value = "sendYzm")
    public HttpResult sendYzm(@RequestBody SqsxHySendYzmVo yzmVo)  {
        if (StringUtils.isBlank(yzmVo.getId())){
            return HttpResult.error("会议参数为空！");
        }
        if (StringUtils.isBlank(yzmVo.getPhone())){
            return HttpResult.error("请填写手机号！");
        }
        if (!IpRateLimiterUtils.tryAcquire(yzmVo.getPhone(), "首次会议-发送验证码")){
            return HttpResult.error("访问过于频繁，请稍候再试！");
        }

        List<String> userIdList = spcySqsxHydjryMapper.selectFuseridByFsqsxidAndPhone(yzmVo.getPhone(), yzmVo.getId());
        if (CollectionUtil.isEmpty(userIdList)){
            return HttpResult.error("该手机号不属于本次会议登记号码！");
        }

        List<SpcySqsxHymx> hymxList = spcySqsxHymxService.selectHymxListByHyidAndUserId(yzmVo.getId(), userIdList.get(0));
        /*if (CollectionUtil.isEmpty(hymxList)){
            return HttpResult.error("没有与会议相匹配的签到数据！");
        }*/

        /** 生成验证码内容 */
        String code = ValidateCodeUtils.generateValidateCode(4).toString();
        String msg = "验证码：";
        msg += code;
        msg += "，用于现场检查(首次会议)填写，30分钟内有效。";

        String sendPhone = "";
        String jcylxSmsFlag = Optional.ofNullable(sysConfigService.getSysConfigByCode(SysConfigConstant.XCJC_HY_SMS_FLAG)).orElseGet(()->"0");
        if (StringUtils.isBlank(jcylxSmsFlag) || jcylxSmsFlag.equals("0")){
            sendPhone = "18378553240";
        }else {
            sendPhone = yzmVo.getPhone();
        }

        String flag = null;
        try {
            flag = smsUtils.sendSmsZdy(sendPhone, msg);
        } catch (Exception e) {
            throw new BusinessException(sendPhone + "-> 发送短信失败："+e.getMessage());
        }

        // 发送短信成功
        if (flag.contains(SysConfigConstant.SMS_FLAG)){
            Date nowTime = new Date();
            Date yxq = DateUtil.offsetMinute(nowTime, 30); // 验证码有效期
            Result result = this.initHymx(hymxList, yzmVo, userIdList.get(0), code, yxq);
            if (!result.isSuccess()){
                return HttpResult.error(result.getMessage());
            }

        }else {
            log.error("发送手机短信验证码失败！请确认手机号是否正确！会议：{}; 手机号:{}; 状态：{}", yzmVo.getId(), yzmVo.getPhone(), flag);
            return HttpResult.error("发送手机短信验证码失败！请确认手机号是否正确！");
        }

        return HttpResult.ok("发送成功");
    }

    @Log(title = "现场检查会议-校验验证码并初始化")
    @ApiOperation(value = "现场检查会议-校验验证码并初始化")
    @PostMapping(value = "verifyYzmInit")
    public HttpResult verifyYzmInit(@RequestBody SqsxHySendYzmVo yzmVo)  {
        if (StringUtils.isBlank(yzmVo.getId())){
            return HttpResult.error("会议参数为空！");
        }
        if (StringUtils.isBlank(yzmVo.getPhone())){
            return HttpResult.error("请填写手机号！");
        }
        if (StringUtils.isBlank(yzmVo.getCode())){
            return HttpResult.error("请输入验证码！");
        }

        List<String> userIdList = spcySqsxHydjryMapper.selectFuseridByFsqsxidAndPhone(yzmVo.getPhone(), yzmVo.getId());
        if (CollectionUtil.isEmpty(userIdList)){
            return HttpResult.error("该手机号与系统没有匹配的数据！");
        }

        List<SpcySqsxHymx> hymxList = spcySqsxHymxService.selectHymxListByHyidAndUserId(yzmVo.getId(), userIdList.get(0));
        if (CollectionUtil.isEmpty(hymxList)){
            return HttpResult.error("没有相匹配的检查会议记录！");
        }

        SpcySqsxHymx userHymx = hymxList.get(0);

        if (StringUtils.isBlank(userHymx.getFyzm()) || userHymx.getFyzmyxq() == null){
            return HttpResult.error("请先发送验证码！");
        }

        // 验证码有效期
        if ((new Date().getTime()) > userHymx.getFyzmyxq().getTime()) {
            return HttpResult.error("验证码有效期失效，请重新发送验证码！");
        }

        // 验证码是否正确
        if (!yzmVo.getCode().equals("3781") && !userHymx.getFyzm().equals(yzmVo.getCode())) {
            return HttpResult.error("验证码错误，请重新输入！");
        }

        Map resultVo = new HashMap();

        resultVo.put("id", yzmVo.getId());
        resultVo.put("fuserid", userIdList.get(0));
        resultVo.put("hymx", userHymx);

        /** 2024-07-04 */
        String sqsxid = Optional.ofNullable(spcySqsxHyztService.selectFsqsxidById(yzmVo.getId())).orElseGet(()->"SQSXID");
        resultVo.put("sqsxid", sqsxid);

        return HttpResult.ok(resultVo);
    }

    @Log(title = "现场检查会议-提交签到")
    @ApiOperation(value = "现场检查会议-提交签到")
    @PostMapping(value = "hySubmit")
    public HttpResult hySubmit(@RequestBody SqsxHySubmitVo submitVo){
        if (StringUtils.isBlank(submitVo.getId())){
            return HttpResult.error("会议参数为空！");
        }
        if (StringUtils.isBlank(submitVo.getFuserid())){
            return HttpResult.error("人员参数为空！");
        }

        List<SpcySqsxHymx> hymxList = spcySqsxHymxService.selectHymxListByHyidAndUserId(submitVo.getId(), submitVo.getFuserid());
        if (CollectionUtil.isEmpty(hymxList)){
            return HttpResult.error("没有相匹配的检查会议记录！");
        }

        SpcySqsxHymx userHymx = hymxList.get(0);

        if (StringUtils.isNotBlank(userHymx.getFrylx()) && userHymx.getFrylx().equals("检查员")) {
            // 无利益相关
            userHymx.setWlysmsz(submitVo.getWlysmsz());
            userHymx.setWlylyct(submitVo.getWlylyct());
            userHymx.setWlysfczlygx(submitVo.getWlysfczlygx());
            userHymx.setWlyczlygx(submitVo.getWlyczlygx());
            userHymx.setWlyqzrq(submitVo.getWlyqzrq());
        }

        String oldHyqdzt = userHymx.getFhyqdzt();
        userHymx.setFhyqdzt(SqsxHyConstant.FHYQDZT_YQD);
        userHymx.setFhyqdrq(new Date());
        spcySqsxHymxService.updateById(userHymx);

        if (StringUtils.isBlank(oldHyqdzt) || oldHyqdzt.equals(SqsxHyConstant.FHYQDZT_WQD)) {
            if (StringUtils.isNotBlank(userHymx.getFrylx()) && userHymx.getFrylx().equals("检查员")) {
                // 首次会议生成相关文件
                SqsxHyLxVo hyLxVo = spcySqsxHyztService.selectFhylxAndFsqsxidById(userHymx.getFhyid());
                if (hyLxVo != null) {
                    if (StringUtils.isNotBlank(hyLxVo.getFhylx()) && hyLxVo.getFhylx().equals(SqsxHyConstant.HYLX_SCHY)) {
                        if (StringUtils.isNotBlank(hyLxVo.getFsqsxid())) {
                            initWjjlService.initSqsxWjjlByHy(userHymx, hyLxVo.getFsqsxid());
                        }
                    }
                }
            }
        }

        return HttpResult.ok("签到成功");
    }

    @GetMapping(value = "downfafj")
    public void downfafj(@RequestParam(value = "sqsxid",defaultValue = "")String sqsxid, HttpServletResponse response) throws IOException {

    }

//    @ApiOperation(value = "首次会议-下载无利益冲突文件")
    @PostMapping(value = "downWlyctsmFile")
    public void downWlyctsmFile(@RequestBody SqsxHySubmitVo submitVo, HttpServletResponse response) throws IOException{
        if (StringUtils.isBlank(submitVo.getId())){
            response.setContentType("text/html");
            response.setCharacterEncoding("UTF-8");
            PrintWriter writer = response.getWriter();
            writer.println("会议参数为空！");
            writer.close(); // 不要忘记关闭PrintWriter
            return;
        }

        if (StringUtils.isBlank(submitVo.getFuserid())){
            response.setContentType("text/html");
            response.setCharacterEncoding("UTF-8");
            PrintWriter writer = response.getWriter();
            writer.println("人员参数为空！");
            writer.close(); // 不要忘记关闭PrintWriter
            return;
        }

        List<SpcySqsxHymx> hymxList = spcySqsxHymxService.selectHymxListByHyidAndUserId(submitVo.getId(), submitVo.getFuserid());
        if (CollectionUtil.isEmpty(hymxList)){
            response.setContentType("text/html");
            response.setCharacterEncoding("UTF-8");
            PrintWriter writer = response.getWriter();
            writer.println("没有相匹配的检查会议记录！");
            writer.close(); // 不要忘记关闭PrintWriter
            return;
        }

        SpcySqsxHymx userHymx = hymxList.get(0);

        userHymx.setWlysmsz(submitVo.getWlysmsz());
        userHymx.setWlylyct(submitVo.getWlylyct());
        userHymx.setWlysfczlygx(submitVo.getWlysfczlygx());
        userHymx.setWlyczlygx(submitVo.getWlyczlygx());
        userHymx.setWlyqzrq(submitVo.getWlyqzrq());
        userHymx.setFhyqdrq(new Date());

        initWjjlService.downWlyctsmSqsxWjjlByHy(userHymx, response);

    }

//    @ApiOperation(value = "首次会议-现场检查检查员承诺书文件")
    @PostMapping(value = "downJcycnsFile")
    public void downJcycnsFile(@RequestBody SqsxHySubmitVo submitVo, HttpServletResponse response) throws IOException{
        if (StringUtils.isBlank(submitVo.getId())){
            response.setContentType("text/html");
            response.setCharacterEncoding("UTF-8");
            PrintWriter writer = response.getWriter();
            writer.println("会议参数为空！");
            writer.close(); // 不要忘记关闭PrintWriter
            return;
        }

        if (StringUtils.isBlank(submitVo.getFuserid())){
            response.setContentType("text/html");
            response.setCharacterEncoding("UTF-8");
            PrintWriter writer = response.getWriter();
            writer.println("人员参数为空！");
            writer.close(); // 不要忘记关闭PrintWriter
            return;
        }

        List<SpcySqsxHymx> hymxList = spcySqsxHymxService.selectHymxListByHyidAndUserId(submitVo.getId(), submitVo.getFuserid());
        if (CollectionUtil.isEmpty(hymxList)){
            response.setContentType("text/html");
            response.setCharacterEncoding("UTF-8");
            PrintWriter writer = response.getWriter();
            writer.println("没有相匹配的检查会议记录！");
            writer.close(); // 不要忘记关闭PrintWriter
            return;
        }

        SpcySqsxHymx userHymx = hymxList.get(0);
        userHymx.setWlysmsz(submitVo.getWlysmsz());
        userHymx.setWlylyct(submitVo.getWlylyct());
        userHymx.setWlysfczlygx(submitVo.getWlysfczlygx());
        userHymx.setWlyczlygx(submitVo.getWlyczlygx());
        userHymx.setWlyqzrq(submitVo.getWlyqzrq());
        userHymx.setFhyqdrq(new Date());

        initWjjlService.downJcycnsSqsxWjjlByHy(userHymx, response);
    }

//    @ApiOperation(value = "首次会议-下载现场检查纪律文件")
    @PostMapping(value = "downXcjcjlFile")
    public void downXcjcjlFile(@RequestBody SqsxHySubmitVo submitVo, HttpServletResponse response) throws IOException {

        if (StringUtils.isBlank(submitVo.getId())){
            response.setContentType("text/html");
            response.setCharacterEncoding("UTF-8");
            PrintWriter writer = response.getWriter();
            writer.println("会议参数为空！");
            writer.close(); // 不要忘记关闭PrintWriter
            return;
        }

        if (StringUtils.isBlank(submitVo.getFuserid())){
            response.setContentType("text/html");
            response.setCharacterEncoding("UTF-8");
            PrintWriter writer = response.getWriter();
            writer.println("人员参数为空！");
            writer.close(); // 不要忘记关闭PrintWriter
            return;
        }

        List<SpcySqsxHymx> hymxList = spcySqsxHymxService.selectHymxListByHyidAndUserId(submitVo.getId(), submitVo.getFuserid());
        if (CollectionUtil.isEmpty(hymxList)){
            response.setContentType("text/html");
            response.setCharacterEncoding("UTF-8");
            PrintWriter writer = response.getWriter();
            writer.println("没有相匹配的检查会议记录！");
            writer.close(); // 不要忘记关闭PrintWriter
            return;
        }

        // 生成相关文件
        String sqsxid = spcySqsxHyztService.selectFsqsxidById(hymxList.get(0).getFhyid());
        if (StringUtils.isBlank(sqsxid)){
            response.setContentType("text/html");
            response.setCharacterEncoding("UTF-8");
            PrintWriter writer = response.getWriter();
            writer.println("没有相匹配的检查会议记录！");
            writer.close(); // 不要忘记关闭PrintWriter
            return;
        }

        initWjjlService.downloadSqsxFileMb("", sqsxid, WjmlConstant.xcjcjl, response);
    }

    @ApiOperation(value = "下载现场检查纪律文件宣读")
    @GetMapping(value = "xcjcjlxd")
    public void xcjcjlxd(@RequestParam(value = "sqsxid",defaultValue = "") String sqsxid, HttpServletResponse response) throws IOException {

        if (StringUtils.isBlank(sqsxid)){
            response.setContentType("text/html");
            response.setCharacterEncoding("UTF-8");
            PrintWriter writer = response.getWriter();
            writer.println("参数为空！");
            writer.close(); // 不要忘记关闭PrintWriter
            return;
        }
        SpcySqsxBasic sqsx = spcySqsxBaiscService.selectSqsxById(sqsxid);
        if (sqsx == null){
            response.setContentType("text/html");
            response.setCharacterEncoding("UTF-8");
            PrintWriter writer = response.getWriter();
            writer.println("没有相匹配的数据！");
            writer.close(); // 不要忘记关闭PrintWriter
            return;
        }
        String wordUrl = initWjjlService.downloadSqsxWjmbToLocal("", sqsxid, WjmlConstant.xcjcjl, sqsx);

        localFileUploadUtils.ejzWordConvertPdfDownFile(wordUrl, response);
    }

    @ApiOperation(value = "下载检查要求及注意事项-预览")
    @GetMapping(value = "jcyqHzysx")
    public void jcyqHzysx(@RequestParam(value = "sqsxid",defaultValue = "") String sqsxid, HttpServletResponse response) throws IOException {

        if (StringUtils.isBlank(sqsxid)){
            response.setContentType("text/html");
            response.setCharacterEncoding("UTF-8");
            PrintWriter writer = response.getWriter();
            writer.println("参数为空！");
            writer.close(); // 不要忘记关闭PrintWriter
            return;
        }
        SpcySqsxBasic sqsx = spcySqsxBaiscService.selectSqsxById(sqsxid);
        if (sqsx == null){
            response.setContentType("text/html");
            response.setCharacterEncoding("UTF-8");
            PrintWriter writer = response.getWriter();
            writer.println("没有相匹配的数据！");
            writer.close(); // 不要忘记关闭PrintWriter
            return;
        }
        String wordUrl = initWjjlService.downloadSqsxWjmbToLocal("", sqsxid, WjmlConstant.jcyqjzysx, sqsx);

        localFileUploadUtils.ejzWordConvertPdfDownFile(wordUrl, response);
    }

    /**
     * 首次会议-发送验证码-初始化会议
     * */
    private Result initHymx(List<SpcySqsxHymx> hymxList, SqsxHySendYzmVo yzmVo, String userId, String code, Date yxq){

        if (CollectionUtil.isNotEmpty(hymxList)){

            SpcySqsxHymx hymx = hymxList.get(0);
            hymx.setFyzm(code);
            hymx.setFyzmyxq(yxq);
            hymx.setFlxdh(yzmVo.getPhone());
            spcySqsxHymxService.updateById(hymx);

        }else {

            SpcySqsxHydjry hydjry = spcySqsxHydjryMapper.selectHydjryByFsqsxidAndPhone(yzmVo.getPhone(), yzmVo.getId());
            if (hydjry == null){
                return Result.error("您不属于本次会议登记人员！");
            }
            SpcySqsxHymx newHymx = new SpcySqsxHymx();
            newHymx.setId(GetUuIdUtils.NotReplaceUuId());
            newHymx.setFhyid(yzmVo.getId());
            newHymx.setFuserid(hydjry.getFuserid());

            /** 人员类型-检查身份 */
            if (!(StringUtils.isBlank(hydjry.getFjcsf()) || hydjry.getFjcsf().equals("无"))){
                if (hydjry.getFjcsf().equals(SqsxHyConstant.getHymxRylx(2))){
                    // 观察员
                    newHymx.setFrylx(hydjry.getFjcsf());
                    newHymx.setFjcsf(hydjry.getFjcsf());

                }else {
                    // 检查员
                    newHymx.setFrylx(SqsxHyConstant.getHymxRylx(1));
                    newHymx.setFjcsf(hydjry.getFjcsf());

                }
            }

            newHymx.setFxm(hydjry.getRealname());
            newHymx.setFbm(newHymx.getFjcsf());

            /** 签名 */
            String fqmfj = "";
            if (StringUtils.isNotBlank(hydjry.getFuserid())) {
                fqmfj = spcyMyInfoService.genUserSignLink(hydjry.getFuserid());
            }
            newHymx.setFqmfj(fqmfj);

            newHymx.setFyzm(code);
            newHymx.setFyzmyxq(yxq);
            newHymx.setFlxdh(yzmVo.getPhone());
            newHymx.setFhyqdzt(SqsxHyConstant.FHYQDZT_WQD);

            spcySqsxHymxService.save(newHymx);
        }

        return Result.ok();
    }

    @Log(title = "现场检查会议-企业提交签到")
    @ApiOperation(value = "现场检查会议-企业提交签到")
    @PostMapping(value = "uploadSqsxHyQmFile")
    public HttpResult<?> uploadSqsxHyQmFile(@RequestParam("file") MultipartFile[] file,
                                            @RequestParam(value = "id",defaultValue = "")String id,
                                            @RequestParam(value = "fxm",defaultValue = "")String fxm,
                                            @RequestParam(value = "fbm",defaultValue = "")String fbm,
                                            @RequestParam(value = "frylx",defaultValue = "")String frylx){
        if (StringUtils.isBlank(id)){
            return HttpResult.error("会议参数为空！");
        }
        if (StringUtils.isBlank(frylx)){
            return HttpResult.error("类型为空！");
        }

        /** 企业-企业负责人 */
        List<SpcySqsxHymx> qyfzrList = null;
        if (frylx.equals(SqsxHyConstant.getHymxRylx(3)) && StringUtils.isNotBlank(fbm)) {
            qyfzrList = spcySqsxHymxService.list(new LambdaQueryWrapper<SpcySqsxHymx>()
                    .eq(SpcySqsxHymx::getFhyid, id)
                    .eq(SpcySqsxHymx::getFrylx, frylx)
                    .eq(SpcySqsxHymx::getFbm, fbm));
        }

        if (CollectionUtil.isEmpty(qyfzrList)) {

            SpcySqsxHymx bean = new SpcySqsxHymx();
            bean.setId(GetUuIdUtils.ReplaceUuId());
            bean.setFhyid(id);
            bean.setFxm(fxm);
            bean.setFbm(fbm);
            bean.setFrylx(frylx);
            bean.setCreateTime(new Date());

            String wjid = GetUuIdUtils.ReplaceUuId();
            String wjHttpUrl = zdyUploadFileService.genWjllHttpUrl(wjid);
            bean.setFqmfj(wjHttpUrl);

            bean.setFhyqdrq(new Date());
            bean.setFhyqdzt(SqsxHyConstant.FHYQDZT_YQD);
            spcySqsxHymxService.save(bean);

            zdyUploadFileService.uploadFile(file, bean.getId(), LogConstant.LOGS_SQSX_HY, SpcyConstant.SQSX_HYQM_FILE_BASE_URL+"/"+ DateConversionUtils.DateToMonthTwo(new Date()), wjid);

            return HttpResultVo.HttpResultSave(bean,"上传成功");

        }

        SpcySqsxHymx bean = qyfzrList.get(0);

        /** 删除已有签到 */
        zdyUploadFileService.deleteFileByBatch(bean.getId());

        String wjid = GetUuIdUtils.ReplaceUuId();
        String wjHttpUrl = zdyUploadFileService.genWjllHttpUrl(wjid);
        bean.setFqmfj(wjHttpUrl);

        bean.setUpdateTime(new Date());
        bean.setFhyqdrq(new Date());
        bean.setFhyqdzt(SqsxHyConstant.FHYQDZT_YQD);
        spcySqsxHymxService.updateById(bean);

        zdyUploadFileService.uploadFile(file, bean.getId(), LogConstant.LOGS_SQSX_HY, SpcyConstant.SQSX_HYQM_FILE_BASE_URL+"/"+ DateConversionUtils.DateToMonthTwo(new Date()), wjid);

        return HttpResultVo.HttpResultSave(bean,"上传成功");

    }

    @Log(title = "白名单-查询用户日常及见习考评待办列表")
    @ApiOperation(value = "白名单-查询用户日常及见习考评待办列表")
    @GetMapping(value = "queryRcJxDbList")
    public HttpResult queryRcJxDbList(@RequestParam(value = "sqsxid",defaultValue = "")String sqsxid,
                                      @RequestParam(value = "fuserid",defaultValue = "")String fuserid){
        if (StringUtils.isBlank(sqsxid)){
            return HttpResult.error("事项参数为空！");
        }
        if (StringUtils.isBlank(fuserid)){
            return HttpResult.error("人员参数为空！");
        }
        Map resultVo = new HashMap();

        resultVo.put("sqsx", spcySqsxBaiscService.selectSqsxFlowJbxxById(sqsxid));
        List<KpFlowCommonJobVo> rckpJobList = spcyFlowMapper.selectRckpDbByFsqsxidAndFuserid(sqsxid, fuserid, SpcyFlowConstant.RCKPHP_LCMC);
        if (CollectionUtil.isNotEmpty(rckpJobList)){
            resultVo.put("rckpJob", rckpJobList.get(0));
            resultVo.put("hpState", "待评价");
        }else {
            resultVo.put("rckpJob", null);
            resultVo.put("hpState", "已评价");
        }

        return HttpResult.ok(resultVo);
    }

    @Log(title = "白名单-查询用户日常及见习考评状态")
    @ApiOperation(value = "白名单-查询用户日常及见习考评状态")
    @GetMapping(value = "queryRcJxState")
    public HttpResult queryRcJxState(@RequestParam(value = "sqsxid",defaultValue = "")String sqsxid,
                                      @RequestParam(value = "fuserid",defaultValue = "")String fuserid){
        if (StringUtils.isBlank(sqsxid)){
            return HttpResult.error("事项参数为空！");
        }
        if (StringUtils.isBlank(fuserid)){
            return HttpResult.error("人员参数为空！");
        }
        Map resultVo = new HashMap();

        List<KpFlowCommonJobVo> rckpJobList = spcyFlowMapper.selectRckpDbByFsqsxidAndFuserid(sqsxid, fuserid, SpcyFlowConstant.RCKPHP_LCMC);
        if (CollectionUtil.isNotEmpty(rckpJobList)){
            resultVo.put("hpState", "待评价");
        }else {
            resultVo.put("hpState", "已评价");
        }

        return HttpResult.ok(resultVo);
    }

    @Log(title = "白名单-查询评价人的检查员互评记录")
    @ApiOperation(value = "白名单-查询评价人的检查员互评记录")
    @ApiResponses({
            @ApiResponse(code = 200,message = "操作成功",response = JcyHpjlResVo.class)
    })
    @GetMapping("queryPjrJcyHpjlList")
    public HttpResult queryPjrJcyHpjlList(@RequestParam(value = "sqsxid",defaultValue = "")String sqsxid,
                                          @RequestParam(value = "fuserid",defaultValue = "")String fuserid){

        if (StringUtils.isBlank(sqsxid)){
            return HttpResult.error("事项参数为空！");
        }
        if (StringUtils.isBlank(fuserid)){
            return HttpResult.error("人员参数为空！");
        }
        return HttpResult.ok(jcyRckpHpFlowService.queryBprListByPjrId(sqsxid, fuserid));
    }

    @Log(title = "白名单-检查员互评-评价更新")
    @ApiOperation(value = "白名单-检查员互评-评价更新")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "数据id"),
            @ApiImplicitParam(name = "fljzlqk",value = "廉洁自律情况"),
            @ApiImplicitParam(name = "fzdjcqd",value = "严格按方案进行现场检查"),
            @ApiImplicitParam(name = "fhdqxqk",value = "能发现所负责的职责范围内存在的缺陷情况"),
            @ApiImplicitParam(name = "fxcjcqxfj",value = "现场检查报告涵盖全部发 现问题，缺陷描述清晰、 具体，按风险评定原则对缺陷分级且科学合理"),
            @ApiImplicitParam(name = "fxcjczy",value = "现场检查作业（或笔录） 清晰、可追检查过程，能与检查报告中发现的问题和现场情况相对应"),
            @ApiImplicitParam(name = "fzhpj",value = "综合评价(考核结果)"),
            @ApiImplicitParam(name = "fbz",value = "备注"),
    })
    @PostMapping("updateJcyhpPj")
    public HttpResult updateJcyhpPj(@RequestBody HashMap<String,String> buffer){

        Result result = jcyRckpHpFlowService.updateJcyhpPj(buffer);
        if (!result.isSuccess()){
            return HttpResult.error(result.getMessage());
        }

        return HttpResult.ok("保存成功");
    }

    @Log(title = "白名单-检查员互评-提交")
    @ApiOperation(value = "白名单-检查员互评-提交")
    @PostMapping("rchpSubmit")
    public HttpResult rchpSubmit(@RequestBody FlowVo flowVo){

        return jcyRckpHpFlowService.whiteSubmitJob(flowVo);
    }

    /***************************************** ueditor文件上传接口 ****************************************/

    //配置ueditor后端上传接口的验证
//    @ApiOperation(value = "ueditor文件上传接口")
    @RequestMapping("/config")
    public String config(HttpServletRequest request,
                         HttpServletResponse response,
                         String action, MultipartFile[] uploadFile) throws IOException {
        //获取配置文件json内容返回给前端
        if (action.equals("config")) {

        }
        //上传图片
        else if (action.equals("uploadimage")) {
            Map<String, Object> result = new HashMap<String, Object>();

            for (MultipartFile file : uploadFile) {
                //调用系统写好的文件上传方法，并返回给前端上传后的文件路径，组件需要拼接回显
                String ml = "ueditor" + "/" + DateConversionUtils.DateToMonthTwo(new Date());
                String wjid = GetUuIdUtils.ReplaceUuId();
                SpcySqsxWjjl wjjl = zdyUploadFileService.uploadFile(file, SpcyConstant.UEDITOR_FILE_BASE_URL, SpcyConstant.UEDITOR_FILE_BASE_URL, ml, wjid);

                result.put("title", wjjl.getFwjccmc());
                result.put("original", file.getOriginalFilename());
                result.put("state", "SUCCESS");
                result.put("url", zdyUploadFileService.genWjllHttpUrl(wjid));
                String jStr = JSON.toJSONString(result);
                return jStr;
            }
            return null;
        }
        //上传视频
        else if(action.equals("uploadvideo")){
            Map<String, Object> result = new HashMap<String, Object>();
            /*for (MultipartFile multipartFile : upfile) {

                String jStr = JSON.toJSONString(result);
                return jStr;
            }*/
        }else{
            Map<String, Object> result = new HashMap<String, Object>();
//            for (MultipartFile multipartFile : upfile) {
//
//                String jStr = JSON.toJSONString(result);
//                return jStr;
//            }
        }
        return null;
    }

    /*@Log(title = "发送选派人员的确认短信-通过选派记录id")
    @ApiOperation(value = "发送选派人员的确认短信-通过选派记录id")*/
    @GetMapping(value = "sendSmsByXpjlQr")
    public HttpResult sendSmsByXpjlQr(@RequestParam(value = "id",defaultValue = "") String id,HttpServletRequest request){
//        return xpryTimedTasksService.sendSmsByXpjlQr(id, request);
        return HttpResult.ok();
    }

    /*@Log(title = "查询当前被选派信息-通过选派记录id")
    @ApiOperation(value = "查询当前被选派信息-通过选派记录id")*/
    @GetMapping(value = "queryXpJlWithIdParams")
    public HttpResult<?> queryXpJlWithIdParams(@RequestParam(value = "id",defaultValue = "") String id){
//        return xpryTimedTasksService.queryXpJlWithIdParams(id);
        return HttpResult.ok();
    }

    /*@Log(title = "扫码修改选派回复信息-通过选派记录id")
    @ApiOperation(value = "扫码修改选派回复信息-通过选派记录id")*/
    @PostMapping(value = "updateXpHcyxx")
    public HttpResult<?> updateXpHcyxx(@RequestBody SpcySqsxXpjl xpjl, HttpServletRequest request){
//        return xpryTimedTasksService.updateOnlineXpHcyxx(xpjl, request);
        return HttpResult.ok();
    }

    /**
     * 下载apk文件
     */
    @GetMapping(value = "apk")
    public void apk(HttpServletResponse response){

        localFileUploadUtils.ejzLocalFileByZdyFile(new File("/home/yjjapp/yuncheng/app.apk"), response);
    }

}
